package com.bll.tsdoa.controller;


import com.alibaba.fastjson.JSON;
import com.bll.tsdoa.convert.PartConvertDtoToEntity;
import com.bll.tsdoa.dto.PartDto;
import com.bll.tsdoa.entity.Part;
import com.bll.tsdoa.entity.Result;
import com.bll.tsdoa.service.PartService;
import com.google.common.base.Preconditions;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 零件(Part)表控制层
 *
 * @author makejava
 * @since 2025-02-20 00:04:36
 */
@RestController
@RequestMapping("/oneDimension/part")
@Slf4j
public class PartController {
    @Resource
    private PartService partService;

    /**
     * 新增零件到数据库
     *
     * @param partDto
     *
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestBody PartDto partDto) {
        if (log.isInfoEnabled()) {
            log.info("PartController.add.dto::{}", JSON.toJSONString(partDto));
        }
        Preconditions.checkNotNull(partDto.getId(), "零件id不能为空");
        Preconditions.checkNotNull(partDto.getLength(), "零件长度不能为空");
        Preconditions.checkNotNull(partDto.getNum(), "零件数量不能为空");
        Part part = PartConvertDtoToEntity.INSTANCE
                .convertDtoToEntity(partDto);
        boolean result = partService.add(part);
        if (result) {
            return Result.success();
        }
        return Result.error("插入失败");
    }

    @DeleteMapping("/delete")
    public Result delete(@RequestBody PartDto partDto) {
        try {
            if (log.isInfoEnabled()) {
                log.info("PartController.delete.dto::{}", JSON.toJSONString(partDto));
            }
            Preconditions.checkNotNull(partDto.getId(), "零件id不能为空");
//            Preconditions.checkNotNull(partDto.getLength(), "零件长度不能为空");
//            Preconditions.checkNotNull(partDto.getNum(), "零件数量不能为空");
            Part part = PartConvertDtoToEntity.INSTANCE
                    .convertDtoToEntity(partDto);
            boolean result = partService.delete(part);
            if (result) {
                return Result.success();
            }
            return Result.error("删除失败");
        } catch (Exception e) {
            log.error(e.getMessage());
            return Result.error(e.getMessage());
        }
    }

    @PutMapping("/update")
    public Result update(@RequestBody PartDto partDto) {
        try {
            if (log.isInfoEnabled()) {
                log.info("PartController.update.dto::{}", JSON.toJSONString(partDto));
            }
            Preconditions.checkNotNull(partDto.getId(), "零件id不能为空");
            Preconditions.checkNotNull(partDto.getLength(), "零件长度不能为空");
            Preconditions.checkNotNull(partDto.getNum(), "零件数量不能为空");
            Part part = PartConvertDtoToEntity.INSTANCE
                    .convertDtoToEntity(partDto);

            boolean result = partService.updateById(part);
            if (result) {
                return Result.success();
            }
            return Result.error("更新失败");
        } catch (Exception e) {
            log.error(e.getMessage());
            return Result.error(e.getMessage());
        }
    }
}

